package com.ruoyi.xp.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.xp.order.service.IOrderItemService;
import com.ruoyi.xp.supply.domain.SupplyWarehouseGoods;
import com.ruoyi.xp.supply.service.ISupplyWarehouseGoodsService;
import com.ruoyi.xp.supply.service.impl.SupplyWarehouseGoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xp.order.mapper.OrderItemGoodsMapper;
import com.ruoyi.xp.order.domain.OrderItemGoods;
import com.ruoyi.xp.order.service.IOrderItemGoodsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 虾皮订单明细仓库货物信息Service业务层处理
 * 
 * @author chdx
 * @date 2023-01-22
 */
@Service
public class OrderItemGoodsServiceImpl implements IOrderItemGoodsService 
{
    @Autowired
    private OrderItemGoodsMapper orderItemGoodsMapper;

    @Autowired
    private ISupplyWarehouseGoodsService supplyWarehouseGoodsService;

    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 查询虾皮订单明细仓库货物信息
     * 
     * @param orderItemGoodsId 虾皮订单明细仓库货物信息主键
     * @return 虾皮订单明细仓库货物信息
     */
    @Override
    public OrderItemGoods selectOrderItemGoodsByOrderItemGoodsId(Long orderItemGoodsId)
    {
        return orderItemGoodsMapper.selectOrderItemGoodsByOrderItemGoodsId(orderItemGoodsId);
    }

    /**
     * 查询虾皮订单明细仓库货物信息列表
     * 
     * @param orderItemGoods 虾皮订单明细仓库货物信息
     * @return 虾皮订单明细仓库货物信息
     */
    @Override
    public List<OrderItemGoods> selectOrderItemGoodsList(OrderItemGoods orderItemGoods)
    {
        return orderItemGoodsMapper.selectOrderItemGoodsList(orderItemGoods);
    }

    /**
     * 新增虾皮订单明细仓库货物信息
     * 
     * @param orderItemGoods 虾皮订单明细仓库货物信息
     * @return 结果
     */
    @Override
    public int insertOrderItemGoods(OrderItemGoods orderItemGoods)
    {
        return orderItemGoodsMapper.insertOrderItemGoods(orderItemGoods);
    }

    /**
     * 修改虾皮订单明细仓库货物信息
     * 
     * @param orderItemGoods 虾皮订单明细仓库货物信息
     * @return 结果
     */
    @Override
    public int updateOrderItemGoods(OrderItemGoods orderItemGoods)
    {
        return orderItemGoodsMapper.updateOrderItemGoods(orderItemGoods);
    }

    /**
     * 批量删除虾皮订单明细仓库货物信息
     * 
     * @param orderItemGoodsIds 需要删除的虾皮订单明细仓库货物信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemGoodsByOrderItemGoodsIds(String orderItemGoodsIds)
    {
        return orderItemGoodsMapper.deleteOrderItemGoodsByOrderItemGoodsIds(Convert.toStrArray(orderItemGoodsIds));
    }

    /**
     * 删除虾皮订单明细仓库货物信息信息
     * 
     * @param orderItemGoodsId 虾皮订单明细仓库货物信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemGoodsByOrderItemGoodsId(Long orderItemGoodsId)
    {
        return orderItemGoodsMapper.deleteOrderItemGoodsByOrderItemGoodsId(orderItemGoodsId);
    }

    /**
     * 根据订单号删除商品明细信息
     *
     * @param orderSn
     */
    @Override
    public void deleteOrderItemGoodsByOrderSn(String orderSn){
        OrderItemGoods orderItemGoods = new OrderItemGoods();
        orderItemGoods.setOrderSn(orderSn);
        List<OrderItemGoods> orderItemGoodsList = orderItemGoodsMapper.selectOrderItemGoodsList(orderItemGoods);
        String[] orderItemGoodsIds = new String[orderItemGoodsList.size()];
        for (int i = 0; i < orderItemGoodsList.size(); i++) {
            orderItemGoodsIds[i] = String.valueOf(orderItemGoodsList.get(i).getOrderItemGoodsId());
        }
        orderItemGoodsMapper.deleteOrderItemGoodsByOrderItemGoodsIds(orderItemGoodsIds);
    }

    /**
     * 获取没有采购价的订单明细
     */
    @Override
    public void updatePurchasePrice() {
        List<OrderItemGoods> orderSnList = orderItemGoodsMapper.selectOrderHaveNoPurchasePrice();
        for (OrderItemGoods orderSn : orderSnList) {
            updatePurchasePrice(orderSn.getOrderSn());
        }
    }

    /**
     * 获取没有采购价的订单明细
     */
    @Override
    public void updatePurchasePrice(String orderSn) {
        OrderItemGoods orderItemGoodsQuery = new OrderItemGoods();
        orderItemGoodsQuery.setOrderSn(orderSn);
        List<OrderItemGoods> orderItemGoodsList = orderItemGoodsMapper.selectOrderItemGoodsList(orderItemGoodsQuery);
        for (OrderItemGoods orderItemGoods : orderItemGoodsList) {
            SupplyWarehouseGoods supplyWarehouseGoods = supplyWarehouseGoodsService.getByGoodSkuId(orderItemGoods.getGoodsId());
            BigDecimal purchasePrice = supplyWarehouseGoods.getPurchasePrice();
            BigDecimal purchaseTotalPrice = purchasePrice.multiply(new BigDecimal(orderItemGoods.getQuantity()));
            orderItemGoods.setPurchasePrice(purchasePrice);
            orderItemGoods.setPurchaseTotalPrice(purchaseTotalPrice);
            orderItemGoodsMapper.updateOrderItemGoods(orderItemGoods);
        }
        orderItemService.updatePurchasePrice(orderSn);
    }
}
